
let code = null
const baseUrl = 'https://xwx.gzzmedu.com:6899/html/yz.html'
Page({
  data: {
    isShowSao: true,
    path: ''
  },
  onLoad (option) {
    let { path } = option
    if (path) {
      path = decodeURIComponent(path)
      this.setData({ path, isShowSao: false })
    } else {
      this.setData({ isShowSao: true })
    }
  },
  getMsgHandle (e) {
    const data = e.detail.data
    code = data.pop().code
  },
  onShareAppMessage (options) {
    const { webViewUrl } = options
    const wxPath = '/pages/workService/authenticityVerification/index'
    if (webViewUrl) { // 在web页面里,webViewUrl在IOS里不是最新的值，所以要用到getMsgHandle
      if (code) {
        const path = encodeURIComponent(`${baseUrl}?code=${code}`)
        return {
          title: `这是我的文件信息，快来看看吧！`,
          path: `${wxPath}?path=${path}`
        }
      } else {
        const path = encodeURIComponent(baseUrl)
        return {
          title: '真伪验证查询',
          path: `${wxPath}?path=${path}`
        }
      }
    } else {
      return {
        title: '真伪验证查询',
        path: wxPath
      }
    }
  },
  /**
   * @see 返回
   */
  back () {
    // 返回到首页的时候应该先校验用户是否登录，没有登录要跳转到login
    wx.reLaunch({
      url: '/pages/home/index?isCheckLogin=true'
    })
  },
  goWeb () {
    code = null
    this.setData({
      isShowSao: false,
      path: baseUrl
    })
  },
  /**
   * @see 扫一扫
   */
  clickScan () {
    wx.scanCode({
      success: (res) => {
        const { errMsg, result } = res
        if (errMsg === 'scanCode:ok') {
          const isBusinessWeb = result.indexOf(baseUrl)
          if (isBusinessWeb !== -1) {
            this.setData({
              isShowSao: false,
              path: result
            })
          } else {
            setTimeout(() => {
              wx.showToast({
                title: '无法识别的二维码',
                icon: 'none'
              })
            }, 500)
          }
        }
      },
      fail: (err) => {
      }
    })
  }
})